import { _decorator, Color, Component, Label, Node, Sprite } from 'cc';

import { Message } from '../entity/Message';
import MessageApi from '../api/MessageApi';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { MessageDetail } from './MessageDetail';
const { ccclass, property } = _decorator;

@ccclass('MessageItem')
export class MessageItem extends Component {
    @property(Sprite)
    bg:Sprite
    @property(Label)
    tittle:Label
    @property(Label)
    time:Label
    @property(Node)
    redDot:Node

    private messageData:Message
    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onDetail, this)
    }

    setData(message:Message) {
        this.messageData = message

        this.tittle.string = message.title
        this.time.string = message.date
        
        if(message.isRead){
            this.redDot.active = false
        } else {
            this.redDot.active = true
        }
    }

    async onDetail(){
       console.log(this.messageData)
       this.redDot.active = false
       MessageApi.read(this.messageData.id)
       let ui = await UIManager.instance.openUI(PrefabEnum.MessageDetail)
       ui.getComponent(MessageDetail).setData(this.messageData)
    }
}


